package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ProductInfo;
import cn.wolfcode.crm.query.ProductInfoQueryObject;
import cn.wolfcode.crm.service.IProductInfoService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("productInfo")
public class ProductInfoController {

    @Autowired
    IProductInfoService productInfoService;
    @RequestMapping("")
    public String index() {
        return "productInfo/list";
    }



    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("productInfo:list")
    @PermissionName("产品列表")
    public PageResult list(ProductInfoQueryObject qo) {
        return productInfoService.query(qo);
    }

    //=============查询所有商品,后面分组查询用到=============
    @RequestMapping("queryProducts")
    @ResponseBody
    public List<ProductInfo> queryProducts() {
        return productInfoService.selectAll();
    }


    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(ProductInfo record) {
        JsonResult result = new JsonResult();
        try {
            productInfoService.insert(record);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存操作失败");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("productInfo:update")
    @PermissionName("产品更新")
    public JsonResult update(ProductInfo record) {
        JsonResult result = new JsonResult();
        try {
            productInfoService.updateByPrimaryKey(record);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更新操作操作失败");
        }
        return result;
    }
    @RequestMapping("changeSate")
    @ResponseBody
    @RequiresPermissions("productInfo:changeSate")
    @PermissionName("产品状态修改")
    public JsonResult changeSate(Long id) {
        JsonResult result = new JsonResult();
        try {
            productInfoService.changeSate(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更新操作操作失败");
        }
        return result;
    }
    @RequestMapping("shutDeduc")
    @ResponseBody
    @RequiresPermissions("productInfo:shutDeduc")
    @PermissionName("产品状态修改")
    public JsonResult shutDeduc(Long id) {
        JsonResult result = new JsonResult();
        try {
            productInfoService.shutDeduc(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更新操作操作失败");
        }
        return result;
    }
    @RequestMapping("queryProductForaiId")
    @ResponseBody
    @RequiresPermissions("productInfo:queryProductForaiId")
    @PermissionName("查询产品数据")
    public List<ProductInfo> queryProductForaiId(Long id) {

        return productInfoService.queryProductForaiId(id);
    }
    @RequestMapping("selectProductForaiId")
    @ResponseBody
    @RequiresPermissions("productInfo:selectProductForaiId")
    @PermissionName("查询产品数据")
    public List<ProductInfo> selectProductForaiId(Long id) {
        List<ProductInfo> pros = new ArrayList<>();
        pros.add(productInfoService.selectByPrimaryKey(id));
        return pros;
    }
}
